<template>
	<view class="uni_box change_password">
		<u-navbar :is-back="false" title="">
			<view class="slot_content">
				<view class="nav_img" @click="backTo">
					<image src="@/static/17.svg"></image>
				</view>
				<view class="nav_title">{{title}}</view>
			</view>
		</u-navbar>
		<view>
			<view class="item">
				<image src="@/static/22.svg"></image>
				<input v-model="phone" :disabled="disabled" placeholder="请输入手机号码">
			</view>
			<view class="item">
				<image src="@/static/23.svg"></image>
				<input type="text" v-model="code" placeholder="请输入验证码">
				<text v-if="isTimer">{{time}}s</text>
				<text class="active" v-else @click="lockSheet">发送验证码</text>
			</view>
			<view class="item">
				<image src="@/static/24.svg"></image>
				<input type="text" password v-model="password" placeholder="请输入密码">
			</view>
			<view class="item">
				<image src="@/static/25.svg"></image>
				<input type="text" password v-model="confirm_password" placeholder="请再次输入密码">
			</view>
			<view class="oper_btn" @click="findPassword">更改登陆密码</view>
		</view>
		<yidunCaptcha ref="captcha" :isSense="false" @verify="handleCaptcha"></yidunCaptcha>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: "修改登陆密码",
				disabled: false,
				phone: "",
				code: "",
				password: "",
				confirm_password: "",

				timer: null,
				time: 60,
				isTimer: false
			}
		},
		onLoad: function(option) {
			if (option.type == 1) {
				this.title = "修改登陆密码";
				this.phone = this.$store.state.info.phone;
				this.disabled = true;
			} else {
				this.title = "找回登陆密码";
				this.phone = "";
			}
		},
		methods: {
			async lockSheet() {
				let phone = this.phone;
				if (!phone) {
					this.$refs.uToast.show({
						title: "请输入手机号码",
						type: "error"
					})
					return
				};
				this.$refs.captcha.verify();
			},
			async handleCaptcha(validate) {
				this.isTimer = true;
				this.timer = setInterval(() => {
					this.time--
					if (this.time < 0) {
						this.isTimer = false;
						this.time = 60;
						clearInterval(this.timer);
					};
				}, 1000);
				await this.$u.api.smsSend({
					phone: this.phone,
					scene: "find_password",
					validate:validate
				});
			},
			async findPassword() {
				await this.$u.api.findPassword({
					phone: this.phone,
					code: this.code,
					password: this.password,
					confirm_password: this.confirm_password
				});
				this.$u.toast("登陆密码重置成功");
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/tabbar/my'
					})
				}, 1000);
			},
			backTo() {
				uni.navigateBack()
			},
			router(url) {
				uni.navigateTo({
					url
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.change_password {
		padding: 0 60rpx;
		box-sizing: border-box;
		overflow: hidden;

		.item {
			height: 100rpx;
			border-radius: 20rpx;
			border: 1px solid #6BD288;
			margin-top: 44rpx;
			padding: 0 24rpx;
			box-sizing: border-box;
			background: #F0FBF3;
			display: flex;
			align-items: center;

			image {
				width: 48rpx;
				height: 48rpx;
				margin-right: 20rpx;
			}

			input {
				flex: 1;
			}

			text {
				color: #D1F1DA;
				font-size: 24rpx;
				font-weight: bold;

				&.active {
					color: #6BD288;
				}
			}
		}

		.return_btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #fff;
			font-size: 28rpx;
			border-radius: 20rpx;
			margin-top: 90rpx;
			background: #ED905B;
		}

		.oper_btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #FFFFFF;
			font-size: 28rpx;
			border-radius: 20rpx;
			margin-top: 40rpx;
			background: #6BD288;

			&:last-child {
				margin-top: 40rpx;
			}
		}
	}
</style>
